/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Credit;
import br.com.jr2tecnologia.model.persistence.DAOException;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.BankAccountRepository;
import br.com.jr2tecnologia.model.repository.CreditRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */
@Stateless
public class BankAccountPersistence implements BankAccountPersistenceLocal{
    
    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private BankAccountRepository bankAccountRepository;
    private CreditRepository creditRepository;

    @PostConstruct
    @Override
    public void initialize(){
        bankAccountRepository = DAOFactory.getBankAccountRepository(em);
        creditRepository = DAOFactory.getCreditRepository(em);
    }

    @Override
    public void recalculateQuantitySmsBuyed(BankAccount bankAccount){
        Company company = bankAccount.getCompany();
        double valueSendSms = company.getValueSendSms();
        double balance = bankAccount.getBalance();

        bankAccount.setQuantitySmsBuyed(Math.round(balance/valueSendSms));
        bankAccountRepository.update(bankAccount);
    }

    @Override
    public void recalculateBalance(BankAccount bankAccount){
        Company company = bankAccount.getCompany();
        double valueSendSms = company.getValueSendSms();
        double quantitySmsBuyed = bankAccount.getQuantitySmsBuyed();

        bankAccount.setBalance(valueSendSms*quantitySmsBuyed);
        bankAccountRepository.update(bankAccount);
    }

    @Override
    public void persist(BankAccount bankAccount){
        Company company = bankAccount.getCompany();
        double valueSendSms = company.getValueSendSms();
        double balance = bankAccount.getBalance();
        bankAccount.setQuantitySmsBuyed(Math.round(balance/valueSendSms));
        bankAccountRepository.persist(bankAccount);
    }

    @Override
    public void update(BankAccount bankAccount){
        bankAccountRepository.update(bankAccount);
    }

    @Override
    public void remove(BankAccount bankAccount){
        bankAccountRepository.remove(bankAccount, bankAccount.getPkBankAccount());
    }

    @Override
    public BankAccount findById(BankAccount bankAccount){
        BankAccount entity = null;
        try{
            entity = bankAccountRepository.findById(bankAccount.getPkBankAccount());
        }catch(DAOException e){
            logger.log(Level.SEVERE, "cannot find login for this name: " + bankAccount.getPkBankAccount(), e);
        }
        return entity;
    }

    @Override
    public BankAccount findByName(String name) throws PersistenceUnitException{
        BankAccount entity = null;
        try{
            entity = bankAccountRepository.findByName(name);
        }catch(DAOException ex){
            logger.log(Level.SEVERE, "cannot find bankAccount for this name: {0}", name);
            throw new PersistenceUnitException(ex);
        }
        return entity;
    }

    @Override
    public List<BankAccount> listBankAccountsOfCompany(Company company){
        return bankAccountRepository.list(new BankAccount(company));
    }

    @Override
    public List<BankAccount> listBankAccount(BankAccount bankAccount){
        return bankAccountRepository.list(bankAccount);
    }

    @Override
    public List<BankAccount> listAllBankAccount(){
        try{
            return bankAccountRepository.listAll();
        }catch(DAOException e){
            logger.log(Level.SEVERE, "cannot list all bankAccount", e);
            throw new DAOException(e);
        }
    }

    @Override
    public List<Credit> listCreditOfBank(BankAccount bankAccount){
        Credit credit = new Credit();
        credit.setBankAccount(bankAccount);
        return creditRepository.list(credit);
    }

    @Override
    public List<Credit> listCreditsOfCompany(Company company){
        Credit credit = new Credit(new BankAccount(company));
        return creditRepository.list(credit);
    }
    
    @Override
    public List<Credit> listAllCredit(){
        return creditRepository.listAll();
    }
}
